<?php
/**
 * ListService.php 
 * Created Time 2016年4月20日 下午6:12:05
 *
 * @author     Xiaodc
 * @copyright  Copyright (c) 2005-2015 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
 
namespace Home\Service;

use Home\Common\Service;

class ListService extends Service {
    
    protected $articleMdl;
    protected $extendMdl;
    protected $status = array(1=>'已发布', 2=>'草稿');
    
    public function __construct()
    {
        parent::__construct();
        $this->articleMdl = D('Home/Article');
        $this->extendMdl = D('Home/ArticleField');
    }
    
    public function getAdminArticleList($postData, $start = 0, $limit = 20, $orderBy = null)
    {
        $filter = array();
        if(isset($postData['title']) && $postData['title'])
        {
            $filter['title'] = array('LIKE', '%'.$postData['title'].'%');
        }
    
        if(isset($postData['cat_id']) && $postData['cat_id'] && $postData['cat_id'] > 0)
        {
            $filter['cid'] = $postData['cat_id'];
        }
        $count = $this->count($filter);
        if(!$count)
        {
            return false;
        }
    
        $fields = 'title,cid,aid,read_num,is_publish,created,updated';
        $list = $this->getArticleList($fields, $filter, $start, $limit, $orderBy);
    
        if(!$list)
        {
            return false;
        }
    
        // 格式化输出数据
        $this->__formatList($list);
        
        return array($count, $list);
    
    }
    
    // 根据条件统计总数
    public function count($filter=array())
    {
        $count = $this->articleMdl
        ->where($filter)
        ->count();
    
        return $count;
    }
    /**
     * 获取多行文章主表数据
     * @param string $field 指定字段
     * @param array $filter
     * @param int $start
     * @param int $limit
     * @return array
     * */
    public function getArticleList($field='*', $filter = array(), $start = 0, $limit = 20, $orderBy = null)
    {
        // 获取主表数据
        $master = $this->articleMdl
        ->field($field)
        ->where($filter)
        ->order($orderBy)
        ->limit($start, $limit)
        ->select();
    
        return $master;
    }
    
    public function getCatList($fields = '*', $filter = array(), $isParent = false)
    {
        $tmp = array();
        $objCatServ = D('Home/Category', 'Service');
        $catList = $objCatServ->getCatList($fields, $filter, $isParent);
       
        return $catList;
    }
    
    private function __formatList(&$list)
    {
        if(!$list)
        {
            return false;
        }
        
        // 获取分类名称
        $cids = array_column($list, 'cid');
        $catList = $this->getCatList('cid,cname', array('cid' => array('IN',$cids)));
        $catList = array_column($catList, 'cname', 'cid');
        foreach ($list as &$val)
        {
            $val['cname'] = !empty($catList[$val['cid']]) ? $catList[$val['cid']] : '未分类';
            $val['status'] = $this->status[$val['is_publish']];
        }
        
        return true;
    }
}
